/**
 * 
 */
package com.wubo.bm.common.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.wubo.bm.common.entity.Response;
import com.wubo.bm.common.service.IResponseService;
import com.wubo.bm.exception.BackendException;
import com.wubo.common.util.Assert;
import com.wubo.sec.SecurityManager;
import com.wubo.sec.core.HttpServletRequestHolder;
import com.wubo.sec.core.UserData;
import com.wubo.sec.model.User;
import com.wubo.sec.web.action.BaseSecAction;

import flexjson.JSONSerializer;

/**
 * @author WuBo
 * @CreateDate 2012-2-24 下午02:11:17
 */
@Controller
public class ResponseAction extends BaseSecAction {
	@Resource
	private IResponseService responseService;
	
	private String content;
	private Integer id;
	private int parentId;
	private int responseType;
	private int typeId;
	private List<Response> responses;
	
	public void emit(){
		try{
			Assert.hasText(content);
			Assert.isTrue(responseType > 0);
			Assert.isTrue(typeId > 0);
			UserData user = SecurityManager.getCurrentUser();
			Assert.notNull(user);
			Response response = new Response();
			response.setEmitDate(new Date(System.currentTimeMillis()));
			response.setAudited(true);
			response.setIp(HttpServletRequestHolder.getIP());
			response.setUser((User)user);
			response.setType(responseType);
			response.setTypeId(typeId);
			response.setContent(content);
			response.setParentId(parentId);
			responseService.save(response);
			jsonSuccess(new JSONSerializer().include("*.user.username", "*.user.userText")
					.exclude("*.user.*").serialize(response), true);
		}catch(RuntimeException e){
			jsonError(null);
			throw e;
		}
	}
	
	public void list(){
		Assert.isTrue(responseType > 0);
		Assert.isTrue(typeId > 0);
		
		responses = responseService.listTree(responseType, typeId, true);
		if(responses == null || responses.size() < 1){
			logger.debug("没有找到【类型："+responseType+"，类型ID："+typeId+"】回复");
			jsonSuccess(null);
		}else{
			jsonSuccess(new JSONSerializer().include("*.user.username", "*.user.userText")
					.exclude("*.user.*").deepSerialize(responses), true);
		}
	}
	
	public void del(){
		try{
			if(id > 0){
				responseService.delete(responseService.get(id));
				jsonSuccess(null);
			}
		}catch(BackendException e){
			jsonError(e.getMessage());
		}catch(RuntimeException e){
			jsonError(null);
			throw e;
		}
	}
	
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public int getResponseType() {
		return responseType;
	}
	public void setResponseType(int responseType) {
		this.responseType = responseType;
	}
	public int getTypeId() {
		return typeId;
	}
	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}
	public List<Response> getResponses() {
		return responses;
	}
	public void setResponses(List<Response> responses) {
		this.responses = responses;
	}
	public int getParentId() {
		return parentId;
	}
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
}
